<!--
 * @Autor: Gct
 * @Description: 文章发布详情页面
 * @Date: 2022-04-18 22:05:28
 * @LastEditors: Gct
 * @LastEditTime: 2022-04-18 22:52:19
-->
<template>
  <div>
    <el-form :model="form">
      <!-- 第一行 -->
      <el-row :gutter="10">
        <el-col :span="18">
          <el-form-item label="标题" label-width="60px">
            <el-input
              clearable
              v-model="form.title"
              placeholder="请输入标题"
            ></el-input>
          </el-form-item>
          <el-form-item label="分类" label-width="60px">
            <el-select v-model="form.categoryId" placeholder="请选择栏目">
              <el-option
                clearable
                v-for="item in article"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="封面">
            <el-upload
              :action="uploadFileURL"
              list-type="picture-card"
              :on-success="handlerSuccess"
              :limit="1"
            >
              <i class="el-icon-plus"></i>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 第一行 -->
      <!-- 第二行 富文本编辑器 -->
      <el-form-item label="正文" label-width="60px">
        <Tinymce v-model="form.content" :height="240"></Tinymce>
      </el-form-item>
      <!-- 第二行 富文本编辑器 -->
      <!-- 第三行 返回按钮 发布按钮 -->
      <div style="text-align: right">
        <el-form-item>
          <el-button type="text" @click="$router.go(-1)">返回</el-button>
          <el-button @click="toSubmit" type="primary">发布</el-button>
        </el-form-item>
      </div>
      <!-- 第三行 返回按钮 发布按钮 -->
    </el-form>
  </div>
</template>
<script>
import { findAll, saveOrUpdate } from "@/api/category.js";
import { showFileURL, uploadFileURL } from "@/utils/config";
import Tinymce from "@/components/Tinymce";
export default {
  components: { Tinymce },
  data() {
    return {
      showFileURL,
      uploadFileURL,
      // 表单对象
      form: {},
      // 所有文章分类
      article: [],
    };
  },
  created() {
    this.findAllCar();
  },
  methods: {
    /**
     * @description: 查询所有文章分类
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async findAllCar() {
      let res = await findAll({
        page: 1,
        pageSizze: 1000,
      });
      this.article = res.data;
    },

    /**
     * @description: 图片上传回调
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    handlerSuccess() {
      this.form.cover = showFileURL + res.data.slice(21);
    },

    /**
     * @description: 发布文章
     * @param {*}
     * @return {*}
     * @author: Gct
     */
    async toSubmit() {
      let res = await saveOrUpdate(this, form);
      this.$notify({
        type: "success",
        message: res.message,
      });
      this.$router.go(-1);
    },
  },
};
</script>